# -*- coding: utf-8 -*-
from itertools import groupby
from operator import attrgetter
from collections import defaultdict


class Model(object):
    def __init__(self, id):
        super(Model, self).__init__()
        self.id = id
    def __unicode__(self):
        return unicode(self.id)

test = []

for i in xrange(5):
    test.append(Model(i))

for i in xrange(10):
    test.append(Model(i))

keyfunc = attrgetter('id')
test = sorted(test, key=keyfunc)
gb = groupby(test, keyfunc)

result = defaultdict(list)

for k, g in gb:
    result[k].extend(list(g))
